<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate ? '修改微信分组' : '添加微信分组'"
    :visible="visible"
    custom-class="ele-dialog-form"
    width="520px"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="86px">
      <el-form-item label="推广平台:" prop="platform">
        <el-select
          v-model="form.platform"
          class="ele-block"
          clearable
          placeholder="请选择推广平台"
          @change="platformChange"
        >
          <el-option
            v-for="item in platformList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="推广市场:" prop="bazaarId">
        <el-select
          v-model="form.bazaarId"
          class="ele-block"
          clearable
          placeholder="请选择推广市场"
        >
          <el-option
            v-for="item in bazaarList"
            :key="item.id"
            :disabled="item.status === 0"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="分组名称:" prop="name">
        <el-input
          v-model="form.name"
          :maxlength="20"
          clearable
          placeholder="请输入分组名称"
        />
      </el-form-item>

      <el-form-item label="备注:" style="margin-bottom: 10px">
        <el-input
          v-model="form.remark"
          :maxlength="200"
          :rows="3"
          clearable
          placeholder="请输入备注信息"
          type="textarea"
        />
      </el-form-item>

      <el-form-item label="状态:">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button :loading="loading" type="primary" @click="save"
        >确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "serviceZoneEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 获取市场传参
    bazaar: Array,
    // 修改回显的数据
    data: Object,
  },

  data() {
    return {
      platformList: [
        { value: 1, label: "百度SEM竞价" },
        { value: 2, label: "百度信息流" },
        { value: 3, label: "巨量引擎" },
        { value: 4, label: "搜狗推广" },
        { value: 5, label: "卧龙推广" },
        { value: 6, label: "360点睛" },
      ],

      form: Object.assign({}, this.data, {
        status: 1,
      }),

      // 表单验证规则
      rules: {
        platform: [
          { required: true, message: "请选择推广平台", trigger: "blur" },
        ],
        bazaarId: [
          { required: true, message: "请选择推广市场", trigger: "blur" },
        ],
        name: [
          {
            required: true,
            type: "string",
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("请输入分组名称"));
              }
              this.$http
                .get("/serviceZone/query?param=" + value)
                .then((res) => {
                  if (res.data.code !== 0 || !res.data.data.length) {
                    return callback();
                  }
                  if (
                    this.isUpdate &&
                    res.data.data[0].url === this.data.DomainUrl
                  ) {
                    return callback();
                  }
                  callback(new Error("分组名称已存在"));
                })
                .catch(() => {
                  callback();
                });
            },
          },
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 市场列表
      bazaarList: [],
      // 市场列表
      tempbazaarList: [],
    };
  },

  watch: {
    data() {
      if (Object.keys(this.data).length > 0) {
        this.setBazaarList(this.data.platform);
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        for (let i in this.form) {
          this.form[i] = null;
        }
        this.form.status = 1;
        this.isUpdate = false;
      }
    },
    bazaar() {
      if (this.bazaar) {
        this.tempbazaarList = this.bazaar;
      }
    },
  },

  methods: {
    platformChange(obj) {
      this.setBazaarList(obj);
    },

    setBazaarList(obj) {
      this.bazaarList = [];
      this.form = Object.assign({}, this.form, {
        bazaarId: null,
      });
      for (let i = 0; i < this.tempbazaarList.length; i++) {
        if (this.tempbazaarList[i].platform === obj) {
          this.bazaarList.push(this.tempbazaarList[i]);
        }
      }
      if (this.bazaarList.length > 1) {
        this.bazaarList.sort(function (a, b) {
          return b.status - a.status;
        });
      }
    },

    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http
            .post(
              "/serviceZone/" + (this.isUpdate ? "update" : "create"),
              this.form
            )
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                if (!this.isUpdate) {
                  for (let i in this.form) {
                    this.form[i] = null;
                  }
                  this.form.status = 1;
                }
                this.updateVisible(false);
                this.$emit("done", this.isUpdate);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },

    updateVisible(value) {
      this.$emit("update:visible", value);
    },
  },
};
</script>
